﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Xml.Serialization;
using MovieStorage;

namespace DVDFilmoteka.SaveloadManagement
{
    class Saver
    {
        public String FileName { get; private set; }

        /// <summary>
        /// Konstruktor
        /// </summary>
        /// <param name="fileName"></param>
        public Saver(String fileName)
        {
           FileName = fileName;
        }

        /// <summary>
        /// Ulozi data do predem zadaneho souboru
        /// </summary>
        /// <param name="version"></param>
        /// <param name="listToSave"></param>
        public void save(String version, List<MovieDataStore12> listToSave)
        {
            StreamWriter lStream = new StreamWriter(FileName);
                        
            try
            {
                String _version = Form1Servant.SaveFileVersion;
                lStream.WriteLine("<Version>" + _version + "</Version>");

                XmlSerializer serializer = new XmlSerializer(listToSave.GetType());                
                serializer.Serialize(lStream, listToSave);                
            }
            catch (Exception)
            {
                throw new Exception("Při ukládání souboru došlo k chybě.");
            }
            finally
            {
                lStream.Close();
            }
        }
    }
}